﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaAttribute;
using Csla.Wpf;
using OpenExpressApp.Command;
using Csla.Core;
using System.ComponentModel;
using System.Windows;
using OpenExpressApp.Template;
using OpenExpressApp.Types;
using Csla;
using OpenExpressApp.WPF.Controls;
using System.Collections.ObjectModel;
using log4net;
using System.Collections;
using OpenExpressApp.MetaModel;
using Avalon.Windows.Controls;
using OpenExpressApp.WPF.Templete;
using OpenExpressApp.Module;
using OpenExpressApp.Module.WPF;
using System.Windows.Controls;
using Ricciolo.Controls;
using System.Reflection;
using System.Windows.Markup;
using System.Diagnostics;
using AvalonDock;
using OpenExpressApp.Module.WPF.Editors;
using Csla.OpenExpressApp;

namespace OpenExpressApp.WPF.Command
{
    /// <summary>
    /// 删除列表中所有的根对象
    /// </summary>
    [Command(CommandNames.DeleteListObject, Label = "删除", ToolbarType = ToolbarType.Main, ModuleType = ModuleType.List, ToolTip = "删除记录")]
    public class DeleteListObjectCommand : WPFListViewCommand
    {
        public override bool CanExecute(ListObjectView view)
        {
            if (base.CanExecute(view) == false)
            {
                return false;
            }
            var bindingList = view.Data as IBindingList;
            if (bindingList == null || bindingList.AllowRemove == false)
            {
                return false;
            }
            return view.CurrentObject != null;
        }
        public override void Execute(ListObjectView view)
        {
            var dataList = view.Data as IBindingList;
            var list = view.SelectedObjects.OfType<BusinessBase>().ToArray();
            for (int i = list.Length - 1; i >= 0; i--)
            {
                var item = list[i];

                //如果是树形,则级联删除所有子节点
                if (item is ITreeNode)
                {
                    var items = (item as ITreeNode).GetRecurChildren();
                    //先删除细记录再删除主记录
                    for (int j = items.Count - 1; j >= 0; j--)
                    {
                        var model = items[j] as BusinessBase;
                        model.Delete();
                        //删除根节点对象集合
                        var parent = (items[j] as ITreeNode).ParentNode;
                        if (null == parent)
                        {
                            (view.Control as MultiObjectTreeView).Roots.Remove(model);
                        }
                    }
                }
                else
                {
                    dataList.Remove(item);
                    item.Delete();
                }
            }

            if (dataList.Count > 0)
            {
                view.CurrentObject = dataList[0];
            }
            else
            {
                view.CurrentObject = null;
            }
        }
    }
}
